package servlet;

import bean.UserBean;
import dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    /**
     * 实现注册账号的功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");//防止中文乱码


        //获取账号和密码
        String name =request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("账号："+name);

        UserBean userBean=new UserBean(name,password);

        UserDao userDao=new UserDao();
        int sta=userDao.findUser(userBean);
        //1代表登录
        request.getSession().setAttribute("staId", "2");
        if(sta==0){
            request.getSession().setAttribute("sta", "登录失败，请注册");
            response.sendRedirect("index.jsp");
        }else{
            request.getSession().setAttribute("staId", "3");
            response.sendRedirect("manage.jsp");
        }


    }
}
